package com.joinway.framework.admin.core.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.joinway.framework.admin.bean.domain.AdminUser;
import com.joinway.framework.admin.bean.domain.UserProfile;
import com.joinway.framework.admin.core.bean.MessagePushResult;
import com.joinway.framework.admin.core.bean.PushKey;
import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.view.View;
import com.joinway.framework.core.plugin.PluginManager;
import com.joinway.framework.data.jdbc.repository.TableRepository;

@Service
public class AdminMessageService {

	private final static Logger log = LoggerFactory.getLogger(AdminMessageService.class);

	@Autowired protected TableRepository repository;

	@Autowired protected MessagePushService messageService;
	
	@Autowired(required=false) protected String userProfileServiceName = "";
	
	@Autowired protected List<IUserProfileService> profileServices;
	
	@Transactional(rollbackFor=Throwable.class)
	@InputLog
	public boolean push(String title, String text, String html, int port, AdminUser adminUser) throws Exception {
		AdminUser user = repository.findById(AdminUser.class, adminUser.getId());
		
		MessagePushResult result = messageService.broadcast(title, text, html, user.getLoginName(), port);
		
		boolean android = result.getAndroidResponse() != null ? result.getAndroidResponse().isSuccess() : true;
		boolean ios = result.getIosResponse() != null ? result.getIosResponse().isSuccess() : true;
		
		return android && ios;
	}
	
	@Transactional(rollbackFor=Throwable.class)
	@InputLog
	public View massPush(String title, String text, String html, String[] toUserIds, int port, AdminUser adminUser) throws Exception {
		List<PushKey> keys = buildPushKeys(adminUser.getId(), toUserIds);
		
		messageService.massBroadcast(title, text, html, adminUser.getLoginName(), keys, port);
		
		return new View();
	}
	
	@Transactional(rollbackFor=Throwable.class)
	@InputLog
	public View resendPush(String[] toUserIds, int pageId, String port, AdminUser adminUser) throws Exception {
		List<PushKey> keys = buildPushKeys(adminUser.getId(), toUserIds);
		
		messageService.resendMassBroadcast(adminUser.getLoginName(), keys, pageId);
		
		return new View();
	}
	
	List<PushKey> buildPushKeys(int senderId, String[] toUserIds) throws Exception {
		List<PushKey> keys = new ArrayList<>();

		IUserProfileService profileService = PluginManager.getInstance(profileServices);
		
		for(String userId : toUserIds){
			UserProfile profile = profileService.getUserProfile(userId);
			if(profile != null){
				keys.add(new PushKey(Integer.valueOf(userId), profile.getCellPhoneType()));
			}else{
				log.warn("no user {} profile was found", userId);
			}
		}
		
		return keys;
	}
}


